<?php

/**
 * Created by PhpStorm.
 * user: Evan
 * Date: 16/8/8
 * Time: 上午1:29
 */
class Base extends MY_Public_Api_Controller
{
    /**
     * 用户注册
     */
    public function register()
    {
        $mobile      = trim($this->input->get_post('mobile'));
        $uname       = trim($this->input->get_post('uname'));
        $code        = trim($this->input->get_post('code'));
        $password    = trim($this->input->get_post('password'));
        $openid      = trim($this->input->get_post('openid'));
        $invite_code = trim($this->input->get_post('invite_code'));


        $param = array
        (
            'mobile'      => $mobile,
            'nickname'    => $uname,
            'password'    => $password,
            'code'        => $code,
            'openid'      => $openid,
            'recommend_code' => $invite_code,
        );
        $this->load->service('user/service_user_base');
        $data = $this->service_user_base->register($param);


        $this->json_output($data['data'], $data['status']);
    }


    /**
     * 用户登陆
     */
    public function login()
    {
        $uname    = trim($this->input->get_post('uname'));
        $password = trim($this->input->get_post('password'));


        $this->load->service('user/service_user_base');
        $data = $this->service_user_base->login($uname, $password);
        
        if($data['data']){
	        $this->load->service('user/service_user_auth');
	        $token = $this->service_user_auth->generate_token($data['data']['uid']);
	        $data['data']['token'] = $token['data']['token'];
        }

        $this->json_output($data['data'], $data['status']);
    }
    
    /**
     * 用户登陆
     */
    public function tls_sig()
    {
    	$uid = intval($this->input->get_post('uid'));
    
    	$this->load->library('im/tls_sig');
        $data = $this->tls_sig->generation($uid);
    
    	$this->json_output($data['data'], $data['status']);
    }

    /**
     * 短信验证码登陆
     */
    public function sms_login()
    {
        $mobile = trim($this->input->get_post('mobile'));
        $code   = trim($this->input->get_post('code'));


        $this->load->service('user/service_user_base');
        $data = $this->service_user_base->sms_login($mobile, $code);
		
        if($data['data']){
        	$this->load->service('user/service_user_auth');
        	$token = $this->service_user_auth->generate_token($data['data']['uid']);
        	$data['data']['token'] = $token['data']['token'];
        }

        $this->json_output($data['data'], $data['status']);
    }
}